package runesim.node.packethandler;

import org.apache.mina.common.IoSession;
import runesim.node.model.*;
import runesim.node.net.Packet;

public class Walking implements PacketHandler {
	
	public void handlePacket(Packet p, IoSession session) {
		Player player = (Player) session.getAttachment();
		int length = p.getLength();
		if(p.getId() == 248)
			length -= 14;
		
		player.getPathHandler().resetPath();
		
		int stepCount = (length - 5) / 2;
		int firstStepX = p.readLEShortA(); // absolute x coordinate
		int[][] wayPointOffsets = new int[stepCount][2];
		
		for(int i = 0; i < stepCount; i++) {
			wayPointOffsets[i][0] = p.readByte();
			wayPointOffsets[i][1] = p.readByte();
		}
		
		int firstStepY = p.readLEShort(); // absolute y coordinate
		@SuppressWarnings("unused")
		boolean isRunning = -p.readByte() == 1; // this doesn't seem to ever be
		// true.
		
		// Add the first point to the walking queue.
		player.getPathHandler().addPoint(Point.location(firstStepX, firstStepY));
		
		for(int i = 0; i < stepCount; i++) {
			wayPointOffsets[i][0] += firstStepX;
			wayPointOffsets[i][1] += firstStepY;
			player.getPathHandler().addPoint(Point.location(wayPointOffsets[i][0], wayPointOffsets[i][1]));
		}
	}
}